/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 
 * System Name        : フレームワーク
 * Description        : 
 * 
 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.cloud.pora.mrm.web.common.util;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * <P>
 * プロパティファイルの値を読み込む
 * </P>
 * 
 * @author Wangjiquan
 * @version 1.0 2010/10/11
 */
public class ResourceUtil {
    private ResourceBundle[] resourceBundle = null;

//
//    private static final String[] strCompResource = { "action_processor",
//            "business_processor", "dao_factory", "database_config" };
//
//    private static final String[] strEndStrings = { "_kanri", "_eigyo",
//            "_seibi" };
    private static final String[] strCompResource = { "action_processor",
            "business_processor", "database_config" };

    private static final String[] strEndStrings = { };


    /**
     * <P>コンストラクター</P>
     * <PRE>
     * プロパティファイルを指定する。
     * </PRE>
     * 
     * @param bundleName プロパティファイル名
     */
    public ResourceUtil(String bundleName) {
        //接尾辞を付ける必要があるかどうかの区分
        boolean blnAdd = false;

        //接尾辞のセット
        for (int i = 0; i < ResourceUtil.strCompResource.length; i++) {
            if (ResourceUtil.strCompResource[i].equals(bundleName)) {
                blnAdd = true;
            }
        }
        if (blnAdd) {
            resourceBundle = new ResourceBundle[ResourceUtil.strEndStrings.length + 1];

            resourceBundle[0] = ResourceBundle.getBundle(bundleName);

            for (int i = 1; i < resourceBundle.length; i++) {
                resourceBundle[i] = ResourceBundle.getBundle(bundleName
                        + ResourceUtil.strEndStrings[i - 1]);
            }

        } else {
            resourceBundle = new ResourceBundle[1];

            resourceBundle[0] = ResourceBundle.getBundle(bundleName);
        }

    }

    /**
     * <P>
     * プロパティファイルの値を読み込む
     * </P>
     * 
     * @param key
     *               読み込むキーワード
     * @return String 読み込んだ値 ない場合はnull
     */
    public String getString(String key) {

        String strResult = null;

        for (int i = 0; i < resourceBundle.length && strResult == null; i++) {
            try {
                strResult = resourceBundle[i].getString(key);
                System.out.println("ResourceUtil.java#getString@【" + i + "】"
                        + "【" + strResult + "】");
            } catch (MissingResourceException e) {
            }
        }

        return strResult;
    }
}